﻿#include "messagebox.h"

TMessageBox::TMessageBox(QWidget *parent) : TDialog(parent)
{
    this->InitWidget();
    this->SetupConnections();
    this->Initialize();

    this->setMaxHide();
    this->resize(300,150);
    this->setObjectName("dialagMessage");
}


TMessageBox::~TMessageBox()
{
    delete labIco;
    delete labInfo;
    delete butOk;
    delete butCancel;
}

void TMessageBox::InitWidget(){

    labIco = new QLabel();
    labIco->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

    labInfo = new QLabel();
    labInfo->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    labInfo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    labInfo->setWordWrap(true);
    labInfo->setStyleSheet("color:#fff;");

    butOk = new QPushButton();
    butOk->setMinimumWidth(70);
    butOk->setText(QChar(0xe656) + QString(" 确定"));

    butCancel = new QPushButton();
    butCancel->setMinimumWidth(70);
    butCancel->setText(QChar(0xe603) + QString(" 取消"));

    QHBoxLayout *infoLayout = new QHBoxLayout();
    infoLayout->addWidget(labIco);
    infoLayout->addWidget(labInfo);

    QHBoxLayout *btnLayout = new QHBoxLayout();
    btnLayout->addStretch();
    btnLayout->addWidget(butOk);
    btnLayout->addWidget(butCancel);

    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->setMargin(10);
    mainLayout->setSpacing(10);
    mainLayout->addLayout(infoLayout);
    mainLayout->addLayout(btnLayout);

    QGroupBox *mainGroupBox = new QGroupBox();
    mainGroupBox->setStyleSheet("margin-top: 0px;");
    mainGroupBox->setLayout(mainLayout);

    this->addWidget(mainGroupBox);
}
void TMessageBox::SetupConnections(){
    connect(butOk, SIGNAL(clicked()), this, SLOT(OnBtnOkClicked()));
    connect(butCancel, SIGNAL(clicked()), this, SLOT(OnBtnCancelClicked()));
}

void TMessageBox::Initialize(){

}

void TMessageBox::SetIcon(const QString &icon){
    labIco->setText(icon);
}

void TMessageBox::SetMessage(const QString &msg, int type)
{

    if (type == 0) {
        labIco->setText(QChar(0xe656));
        labIco->setStyleSheet("color:#A4E82F;font-size:50px;font-weight: bold;");

        butCancel->setVisible(false);
        this->setTitle("提示");
    } else if (type == 1) {
        labIco->setText(QChar(0xe757));
        labIco->setStyleSheet("color:yellow;font-size:50px;font-weight: bold;");

        this->setTitle("提示");
    } else if (type == 2) {
        labIco->setText(QChar(0xe603));
        labIco->setStyleSheet("color:#ff0000;font-size:50px;font-weight: bold;");

        butCancel->setVisible(false);
        this->setTitle("错误");
    }

    labInfo->setText(msg);
}

void TMessageBox::OnBtnOkClicked(){
    done(1);
    this->close();
}
void TMessageBox::OnBtnCancelClicked(){
    this->close();
}
